<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreRoleRequest;
use App\Models\Role;
use App\Services\RoleService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RoleController extends Controller
{

    protected $RoleService;
    public function __construct(RoleService $RoleService)
    {
        $this->RoleService = $RoleService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $parent_id = $Request->parent_id;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        
        $type = Auth::user()->company_id > 0 ? 'company' : 'admin';
        $sql = $this->RoleService->get($search,$parent_id,$type);

        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Role::where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新用户。
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  // parent_id 角色ID
     * @param  // name 角色名称
     * @param  // auth 权限
     * @param  // status 状态
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRoleRequest $Request)
    {
        $POSTONLY = Role::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->RoleService->insert($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data' => $data]);
    }


    public function update(StoreRoleRequest $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的用户']);
        }
        $admin = Role::find($Request->id);
        $admin->name = $Request->name;
        $admin->auth = json_encode($Request->auth);
        $admin->parent_id = $Request->parent_id;
        $admin->status = $Request->status ?? 1;
        $admin->save();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function status(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Role::where('company_id',Auth::user()->company_id)->where('id',$Request->id)->first();
        if(!$data){
            return response()->json(['code' => '404', 'msg' => '记录不存在']);
        }
        $data = $this->RoleService->status($data);
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的用户']);
        }
        $admin = role::find($Request->id);
        $admin->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }
}
